package com.overlord.im.connector.netty.channel;

import com.overlord.im.commons.message.MessageDecoder;
import com.overlord.im.commons.message.MessageEncoder;
import com.overlord.im.commons.message.Protocol;
import com.overlord.im.connector.netty.handler.HeartBeatHandler;
import com.overlord.im.connector.netty.handler.ImServiceHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ImServerChannelInitializer
 * @Author wuwei
 * @Description NONE
 * @Date 2020/5/9 11:20
 **/
@Component
public class ImServerChannelInitializer extends ChannelInitializer<Channel> {
    @Resource
    private Protocol protocol;

    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast("idleHandler", new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
        pipeline.addLast("decoder", new MessageDecoder(protocol));
        pipeline.addLast("encoder", new MessageEncoder(protocol));
        pipeline.addLast("heartBeatHandler", new HeartBeatHandler());
        pipeline.addLast("serverHandler", new ImServiceHandler());
    }
}
